package org.jsbd.boss.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc
 * @Date 2018/7/13 15:55
 * @Author xupengcheng [xupengcheng@zhexinit.com]
 */
public class UserAgentUtil {


    private static List<String> userAgentList;

    static {
        userAgentList = new ArrayList<>();
        InputStream input = UserAgentUtil.class.getClassLoader().getResourceAsStream("useragent.properties");
        InputStreamReader inputReader = null;
        BufferedReader bufferReader = null;
        try {
            inputReader = new InputStreamReader(input);
            bufferReader = new BufferedReader(inputReader);
            String line = null;
            while ((line = bufferReader.readLine()) != null) {
                userAgentList.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputReader.close();
                bufferReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static String getUserAgent() {
        return userAgentList.get((int) (userAgentList.size() * Math.random()));
    }
}
